
//解决ios modal弹框兼容性问题
export const addHidden = () => {
  // const system = window.navigator.userAgent;
  // const isIOS = system.indexOf('iPhone') !== -1;
  // if(!isIOS)return;
  // document.body.style.overflow = 'hidden';
  // document.getElementsByTagName('html')[0].style.overflow = 'hidden';
}

export const removeHidden = ()=>{
  // const system = window.navigator.userAgent;
  // const isIOS = system.indexOf('iPhone') !== -1;
  // const modal = document.getElementsByClassName('am-modal-wrap');//多个modal弹框层
  // if(!isIOS)return;
  // if(modal && modal.length > 1){
  //   return;
  // }; //大于1表示，modal有两个以上， 只剩一个了就表示正在关闭最后一个则要重置了
  // document.body.style.overflow = '';
  // document.getElementsByTagName('html')[0].style.overflow = '';
}

/**
 * [getNameByData] 通过员工编号查询员工名称
 * @param  {[type]} id            [员工编号]
 * @param  {[type]} targetArr     [员工信息数组]
 * @param  {[type]} targetIdKey     [员工信息数组中对应id的key]
 * @param  {[type]} targetNameKey [员工信息数组中对应name的key]
 * @return [String]                 [对应id员工的英文名称]
 */
export const getNameByData = (id, targetArr, targetIdKey, targetNameKey) => {
  let enName = id;
  if(!targetArr){
    return enName;
  }
  for(let item of targetArr){
    if(global._.get(item, targetIdKey) == id){
      enName = global._.get(item, targetNameKey)
    }
  }
  return enName;
}

export const getMastId = () => {
  const {apiMaps} = require('@/utils/maps');
  const queryString = require('query-string');
  const query = queryString.parse(window.location.hash.split('?')[1]);
  const idType = global._.find(apiMaps, item => {
    return item.actionType == query.actionType
  }).query;
  return {
    [idType]: query.id
  };
}

export const getMastKey = () => {
  const {apiMaps} = require('@/utils/maps');
  const queryString = require('query-string');
  const query = queryString.parse(window.location.hash.split('?')[1]);
  return global._.find(apiMaps, item => {
    return item.actionType == query.actionType
  }).key;
}

export const getLabelOfValue = (value, arr)=>{
  let label = '';
  for(let item of arr){
    if(item.value == value){
      label = item.label;
    }
  }
  return label;
}
// 获取链接上的数据
export const getUrlParams = (path) => {
  let href = path || window.location.href;
  var hash = href.split('?');
  var url = hash[1];
  if (hash.length > 2) {
    url = hash[1] + '&' + hash[2];
  }
  if (!url) return {};
  let query = {},
    strs;
  if (url.indexOf("&") > -1) {
    strs = url.split("&");
    for (var i = 0; i < strs.length; i++) {
      query[strs[i].split("=")[0]] = strs[i].split("=")[1];
    }
  } else {
    var key = url.substring(0, url.indexOf("="));
    var value = url.substr(url.indexOf("=") + 1);
    query[key] = decodeURI(value);
  }
  return query;
};

export const validateDate = (date) => {
  if(!date)
    return '';
  if(typeof date === 'number')
    return date;
  // console.log(date.replace(/-/g, '/'))
  return date.replace(/-/g, '/');
};

export const getDateTime = (date) => {
  date = validateDate(date);
  if(!date)
    return 0;
  return new Date(date).getTime();
};

export const requestSuccess = (res) => {
  if(res.data && res.status == 'success'){
    return true;
  }
  return false;
}
